package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.CameraAddDto;
import com.one.group.model.dto.CameraQueryDto;
import com.one.group.model.dto.CameraUpdateDto;
import com.one.group.model.entity.Camera;
import com.one.group.model.vo.CameraVo;
import com.one.group.result.Result;
import com.one.group.service.CameraService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 30
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@RestController
@RequestMapping("/camera")
@Slf4j
@Tag(name = "摄像头接口")
public class CameraController {
    @Resource
    private CameraService cameraService;

    @GetMapping("/info")
    @Operation(summary = "摄像头详情接口")
    public Result getInfo(@RequestParam Long id) {
        System.out.println("当前登录状态: " + StpUtil.isLogin());
        System.out.println("当前登录ID: " + StpUtil.getLoginIdDefaultNull());
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(Camera::getId, id);
        Camera camera = cameraService.getOne(wrapper);
        return camera != null ? ResultUtil.success(camera) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }


    @PostMapping("/list")
    @Operation(summary = "摄像头列表接口")
    public Result list(@RequestBody CameraQueryDto cameraQueryDto) {

        new PageHelper().startPage(cameraQueryDto.getPage(), cameraQueryDto.getLimit());

        List<CameraVo> cameraVoList = cameraService.findCameraByQueryDto(cameraQueryDto);

        PageInfo<CameraVo> cameraVoPageInfo = new PageInfo<>(cameraVoList);

        return ResultUtil.success(cameraVoPageInfo);
    }

    @PostMapping("/add")
    @Operation(summary = "摄像头添加接口")
    public Result add(@RequestBody CameraAddDto cameraAddDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(cameraAddDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = cameraService.addCameraByAddDto(cameraAddDto);
        return result ? ResultUtil.success("新增成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PutMapping("/update")
    @Operation(summary = "摄像头修改接口")
    public Result update(@RequestBody CameraUpdateDto cameraUpdateDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(cameraUpdateDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = cameraService.updateCameraByUpdateDto(cameraUpdateDto);
        return result ? ResultUtil.success("修改成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "摄像头删除接口")
    public Result delete(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = cameraService.deleteCameraByIds(id);
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByBatch")
    @Operation(summary = "摄像头批量删除接口")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(baseDeleteDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = cameraService.deleteCameraWithMantenanceByIds(baseDeleteDto.getIds());
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }


}
